/*
编写 : Hexh
时间 : 2012-7-9 10:48:32
*/
using System;
using System.ComponentModel;
using System.Web.Mvc;
using FsCarInfo.Core.Entity;
using FsCarInfo.Facade;
using HMIS.FrameWork;
using HMIS.Infrastructure.Core.Authority;
using HMIS.Infrastructure.Facade.Authority;
using HMIS.MVC.CommonSupport;
using HMIS.MVC.ViewModel;
using Iesi.Collections.Generic;

namespace HMIS.MVC.Controllers
{
	/// <summary>
	///	 the mvc class of FsGatelogList
	/// </summary>
	public class FsGatelogListController : BaseController
	{

		private readonly IFsGatelogListFacade FsGatelogListFacade;
		private readonly IHospUserFacade HospUserFacade;
		
		public FsGatelogListController(IFsGatelogListFacade FsGatelogListFacade, IHospUserFacade HospUserFacade)
		{
			this.FsGatelogListFacade = FsGatelogListFacade;
			this.HospUserFacade = HospUserFacade;
		}
		
		[Description("访问FsGatelogList管理页面")]
		[ViewPage]
		public ActionResult FsGatelogList()
		{
			return View();
		}
		
		[ExtResult]
		[Description("添加修改FsGatelogList信息")]
		public ActionResult SaveOrUpdate()
		{
			FsGatelogList entity;
			if (String.IsNullOrEmpty(Request["Id"]))
			{
				entity = new FsGatelogList
				{
					LogId = Request["LogId"], 
					GateId = Request["GateId"], 
					ApprUserId = Request["ApprUserId"], 
					ApprState = Request["ApprState"], 
					ImgPath = Request["ImgPath"], 
				};
			}
			else
			{
				// get the entity.
				entity = FsGatelogListFacade.Get(Request["Id"]);
				// set new value..
				entity.LogId = Request["LogId"]; 
				entity.GateId = Request["GateId"]; 
				entity.ApprUserId = Request["ApprUserId"]; 
				entity.ApprState = Request["ApprState"]; 
				entity.ImgPath = Request["ImgPath"]; 
			}			
			// TODO ..设置外键相关信息..
			/*
			string[] authusers = Request["AuthorizeUsers"].Split(',');
			for (int i = 0; i < authusers.Length; i++)
			{
				entity.AuthorizeUsers.Add(HospUserFacade.Load(authusers[i]));
			}
			*/
			// 保存信息
			FsGatelogListFacade.SaveOrUpdate(entity);

			return this.JsonFormat(new ExtResult { success = true, id = entity.Id });
		}
		
		[ExtResult]
		[Description("删除FsGatelogList基本资料")]
		public ActionResult Delete()
		{
			FsGatelogListFacade.Delete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}
		
		[ExtResult]
		[Description("物理删除FsGatelogList基本资料")]
		public ActionResult PhysicsDelete()
		{
			FsGatelogListFacade.PhysicsDelete(Request["Id"]);
			return this.JsonFormat((new ExtResult { success = true }));
		}

		[Description("获取FsGatelogList清单")]
		public ActionResult GetPlistAll()
		{
			long total;
			var data = FsGatelogListFacade.GetPlistAll(
				Request["q_query"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<FsGatelogList> { total = total, data = data },
				new string[] { "FsGatelog" });
		}

		[Description("获取车辆通行记录")]
		public ActionResult GetPlist()
		{
			long total;
			var data = FsGatelogListFacade.GetPlist(
				Request["DepartmentId"],
				Request["GateId"],
				Request["CardUid"],
				Request["ApprState"],
				Request["State"],
				int.Parse(Request["start"] == null ? "0" : Request["start"]),
				int.Parse(Request["limit"] == null ? "0" : Request["limit"]),
				Request["sort"],
				Request["dir"],
				this.DesDataFilter(Request["filter"]),
				out total);
			//TODO : 修改关联查询返回的类型
			return this.JsonFormat(
				new ExtGirdData<FsGatelogList> { total = total, data = data },
				new string[] { "FsGatelog", "FsGateInfo" });
		}
		
		[Description("获取FsGatelogList信息")]
		public ActionResult GetById()
		{
			var entity = FsGatelogListFacade.Get(Request["Id"]);
			return this.JsonFormat(
			   new ExtFormLoadData<FsGatelogList> { data = new FsGatelogList[] { entity } }, new string[] { "AuthorizeUsers" }
			   );
		}

		[ExtResult]
		[Description("判断FsGatelogList字段是否唯一")]
		public ActionResult IsFieldExist()
		{
			var result = FsGatelogListFacade.IsFieldExist(Request["FieldName"], Request[Request["FieldName"]], Request["Id"]);
			return this.JsonFormat(new ExtResult { success = !result, msg = "已存在相同的项!" });
		}
	}
}
